package com.wenlin.myproduction.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.wenlin.myproduction.MyApplication;

import java.util.Map;
import java.util.Set;


public class SharePreferenceUtil {
	private final static String SP_NAME = "ZqwLive";
	private static SharedPreferences sp;
	private static SharedPreferences.Editor editor;
	static SharePreferenceUtil instance;
	public static SharePreferenceUtil getInstance() {
		if(instance == null){
			instance = new SharePreferenceUtil();
		}
		return instance;
	}

	public SharePreferenceUtil() {
		sp = MyApplication.getMyApplicationContext().getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
		editor = sp.edit();
	}

	public static void init() {
		if (sp == null) {
			sp = MyApplication.getMyApplicationContext().getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
			editor = sp.edit();
		}
	}



	public static void cleanAllDada() {
		if (editor != null) {
			editor.clear();
			editor.commit();
		}
	}


	public  boolean putString(String key, String value) {
		editor.putString(key, value);
		return editor.commit();
	}

	public String getString(String key) {
		String str = null;
		str = sp.getString(key, null);
		return str;
	}

	public String getString(String key, String def) {
		String str = def;
		str = sp.getString(key, def);
		return str;
	}

	public  boolean putSet(String key, Set<String> set) {
		editor.putStringSet(key, set);
		return editor.commit();
	}

	public Set<String> getSet(String key) {
		return sp.getStringSet(key, null);
	}

	public  boolean putBoolean(String key, boolean value) {
		editor.putBoolean(key, value);
		return editor.commit();
	}

	public  boolean getBoolean(String key) {
		boolean res = false;
		res = sp.getBoolean(key, false);
		return res;
	}

	public  boolean putInt(String key, int value) {
		editor.putInt(key, value);
		return editor.commit();
	}

	public  int getInt(String key, int def) {
		int str = -1;
		str = sp.getInt(key, def);
		return str;
	}

	public  boolean putFloat(String key, float value) {
		editor.putFloat(key, value);
		return editor.commit();
	}

	public  float getFloat(String key, float def) {
		float str = -1f;
		str = sp.getFloat(key, def);
		return str;
	}

	public  boolean putLong(String key, long value) {
		editor.putLong(key, value);
		return editor.commit();
	}

	public  long getLong(String key, long def) {
		long str = -1l;
		str = sp.getLong(key, def);
		return str;
	}

	// 默认
	public  boolean getBoolean(String key, boolean def) {
		boolean res = def;
		res = sp.getBoolean(key, def);
		return res;
	}

	public  boolean contains(String key) {
		return sp.contains(key);
	}

	public boolean remove(String key) {
		 editor.remove(key);
		return editor.commit();
	}

	public Map<String,?> getAll() {
		return sp.getAll();
	}

}
